import {ref, reactive, toRefs} from "vue";
import {searchStartAndEnd} from "@/utils/auth.js"
export const tableMixin = (opt)=>{
    let {
        getListCallBack=()=>{},
        getListApi,
        queryParams,
        searchTime
    } = opt;

    const state = reactive({
        total: 0,
        ids: [],
        loading: false,
        tableDataList: []
    })

    const publicListRef = ref();
    // 获取当前页码
    const getCurrentPage = (num) => {
        queryParams.pageNum = num
    }
    const handleSelectionChange = (idList) => {
        state.ids = idList;
    }
    const handlePaginateFun = (obj) => {
        if (obj.type == "selectAll") {
            publicListRef.value.tzsTableRef.handAllSelected(state.tableDataList)
        } else if (obj.type == "current") {
            queryParams.pageNum = obj.val;
            getTableData()
        } else if (obj.type == "size") {
            queryParams.pageSize = obj.val;
            getTableData()
        }
    }
    const searchSubmit = () => {
        queryParams.pageNum = 1;
        getTableData()
    }
    // 列表请求
    const getTableData = () => {
        let params = searchStartAndEnd(queryParams,searchTime)
        state.loading = true
        getListApi(params).then((res) => {
            getListCallBack(res)
        }).finally(() => {
            state.loading = false
        })
    }
    return {
        ...toRefs(state),
        publicListRef,
        getTableData,
        getCurrentPage,
        searchSubmit,
        handlePaginateFun,
        handleSelectionChange
    }

}